/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author:  Jorge Pintado de Santiago
 *          
 */


package com.madsa.tuPeli.cartelera;

import java.io.IOException;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.madsa.AugmentedCompass.BrujulaAumentada;
import com.madsa.AugmentedReality.RealidadAumentada;
import com.madsa.maps.MapsActivity;
import com.madsa.maps.MapsRouteNavigator;
import com.sisof.R;

public class CineActivity extends ListActivity {
	private Context context;
    private String selectCinema;

    protected void onListItemClick(ListView l, View v, int position, long id) 
    {    
        String[] cinemaLocations = getResources().getStringArray(R.array.locations);
    	selectCinema = cinemaLocations[position];
    	showDialog(1);
    }

    protected Dialog onCreateDialog(final int id) {
    	final String[] items = {"Mostrar la ruta", "Ver en el mapa", "Brújula aumentada", "Realidad aumentada"};        
    	
    	AlertDialog opciones = new AlertDialog.Builder(CineActivity.this)
    	.setTitle("OPCIONES:")
    	.setItems(items, new DialogInterface.OnClickListener() {
    		public void onClick(DialogInterface dialog, int which) {
    			if (items[which].equals("Mostrar la ruta")){
    			    String[] cineDestino = new String[1];
    			    cineDestino[0] = selectCinema;
    			    Intent intent;
    			    intent = new Intent().setClass(context, MapsRouteNavigator.class);
    			    intent.putExtra("destino", selectCinema);
    			    startActivity(intent);
    			}else if(items[which].equals("Ver en el mapa")){
    			    String[] cineDestino = new String[1];
    			    cineDestino[0] = selectCinema;
    			    Intent intent;
    			    intent = new Intent().setClass(context, MapsActivity.class);
    			    intent.putExtra("destino", cineDestino);
    			    startActivity(intent);
    			}else if(items[which].equals("Brújula aumentada")){
    			    String[] cineDestino = new String[1];
    			    cineDestino[0] = selectCinema;
    			    Intent intent;
    			    intent = new Intent().setClass(context, BrujulaAumentada.class);
    				Geocoder fwdGeocoder = new Geocoder(context);
    				try{
    					Address destineLocation = fwdGeocoder.getFromLocationName(cineDestino[0].split(";")[1], 1).get(0);
    					double destineLat = destineLocation.getLatitude();
    					double destineLng = destineLocation.getLongitude();
    			    	intent.putExtra("destineLocation", destineLat + "," + destineLng);
    				}catch (IOException e) {} 			    

    			    startActivity(intent);
    			}else if(items[which].equals("Realidad aumentada")){
    			    Intent intent;
    			    intent = new Intent().setClass(context, RealidadAumentada.class);
    			    startActivity(intent);
    			}
    		}
    	}).create();
    	return opciones;
    }
    

	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setListAdapter(new OptionListAdapter(this));
        
    }
    
    private class OptionListAdapter extends BaseAdapter {
        private Context mContext;
        
        String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
        "<resources>" + 
        "	<string-array name=\"locations\">" + 
        		"<item>Acteón; Calle Montera 31, Madrid</item>"+
        		"<item>Callao; Plaza del Callao 3, Madrid</item>"+
        		"<item>kinepolis; Edgar Neville Ciudad de la Imagen s/n. Pozuelo de Alarcón, 28223</item>"+	
        	"</string-array>"+
       "</resources>";

        private String[] cinemaLocations = getResources().getStringArray(R.array.locations);
        
        public OptionListAdapter(Context context){
            mContext = context;
        }

        public int getCount() {
            return cinemaLocations.length;
		}

		public Object getItem(int position) {
            return position;
		}

		public long getItemId(int position) {
            return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			
			String title = cinemaLocations[position].split(";")[0];
			
            OptionView sv;
            if (convertView == null) {
                sv = new OptionView(mContext, title);
            } else {
                sv = (OptionView)convertView;
                sv.setTitle(title);
            }
			return sv;
		}
		
	    private class OptionView extends LinearLayout {
	        private TextView mTitle;
	        

	        
	    	public OptionView(Context context, String title) {
	            super(context);	            
	            this.setOrientation(VERTICAL);
	            this.setPadding(20, 20, 20, 20);
	            this.setBackgroundColor(Color.TRANSPARENT);
	            
	            // Here we build the child views in code. They could also have
	            // been specified in an XML file.
	            
	            mTitle = new TextView(context);
	            mTitle.setText(title);
	            mTitle.setTextSize(20); 
	            mTitle.setTextColor(Color.WHITE);
	            addView(mTitle, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
	            		LayoutParams.WRAP_CONTENT));
	    	}
	        

	        public void setTitle(String title) {
	            mTitle.setText(title);
	        }
	    }
    }
    	
}
